package site.whatsblog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import site.whatsblog.pojo.User;
import site.whatsblog.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <strong>
 * description :
 * </strong>
 * <br/>
 * <p>author : Suk-Lee</p>
 * <p>date : 2022/6/5 17:17</p>
 * <p>site : https://www.whatsblog.site/</p>
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        User user1 = userService.selectByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (user1 != null) {
            request.getSession().setAttribute("user", user1);
            request.getSession().setAttribute("users", userService.selectAll());
           return "findAllUsers";
        } else {
            return "login";
        }
    }

    @RequestMapping("/register")
    public String register(User user, HttpServletRequest request) {
        int i = userService.insert(user);
        if (i > 0) {
            request.getSession().setAttribute("user", user);
            request.getSession().setAttribute("users", userService.selectAll());
            return "findAllUsers";
        } else {
            return "index";
        }
    }
}
